class RequestsController < ApplicationController
  before_filter :authenticate_user!
  # GET /requests
  # GET /requests.json
  def index
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
    @requests = Request.all
    @books=@requests.map{|x| x.book}
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @requests }
    end
    end
  end

  # GET /requests/1
  # GET /requests/1.json
  def show
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
    @request = Request.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @request }
    end
    end
  end

  # GET /requests/new
  # GET /requests/new.json
  def new
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
    @request = Request.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @request }
    end
    end
  end

  # GET /requests/1/edit
  def edit
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
    @request = Request.find(params[:id])
  end
  end

  # POST /requests
  # POST /requests.json
  def create
    @request = Request.new()
    @request.user_id = session[:user].id
    @request.book_id = params[:book][:book_id]
    @request.request_time = Time.now
    
   respond_to do |format|
      if @request.save
        if(session[:language] == "vi")
          format.html { redirect_to book_path(Book.find(params[:book][:book_id]))+"?lang=vi", notice: 'Request was sent, please wait reply from us' }
        else
          format.html { redirect_to Book.find(params[:book][:book_id]), notice: 'Request was sent, please wait reply from us' }
        end
        format.json { render json: @request, status: :created, location: @request }
      else
        format.html { render action: "new" }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /requests/1
  # PUT /requests/1.json
  def update
    if session[:user].role != "admin"
      redirect_to homepage_url
    else
    @request = Request.find(params[:id])

    respond_to do |format|
      if @request.update_attributes(params[:request])
        format.html { redirect_to @request, notice: 'Request was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @request.errors, status: :unprocessable_entity }
      end
    end
    end
  end

  # DELETE /requests/1
  # DELETE /requests/1.json
  def destroy
    
    @request = Request.find(params[:id])
    @request.destroy

    respond_to do |format|
      if(session[:language] == "vi")
        format.html { redirect_to borrow_books_url+"?lang=vi" }
      else
        format.html { redirect_to borrow_books_url }
      end
      format.json { head :no_content }
    end
  end
  
  def accept
    
    @request = Request.find(params[:id])
    @book = Book.find(@request.book_id)
    
    # Descre book quantity by 1
    @book.quantity -=1
    
    # Create new Borrow Book
    @borrow_book = BorrowBook.new()
    @borrow_book.code = "New"
    @borrow_book.user_id = @request.user_id
    @borrow_book.book_id = @request.book_id
    @borrow_book.borrow_date = DateTime.now
    @borrow_book.deadline_date = DateTime.now + 14
    
    # Save db
    @borrow_book.save
    @book.save
    
    # Delete request from database
    @request.destroy
    
    # Redirect to Borrow Book
    if(session[:language] == "vi")
      redirect_to borrow_books_url+"?lang=vi"
    else
      redirect_to borrow_books_url
    end
  end
  
  def returnBook
     @borrow_book = BorrowBook.find(params[:bid])
     @book = Book.find(@borrow_book.book_id)
     
     @borrow_book.return_date = Time.now
     
     # Ascend Book quantity
     @book.quantity +=1
     
     # Save to db
     @borrow_book.save
     @book.save
     
     # Redirect to Borrow_book
     if(session[:language] == "vi")
      redirect_to borrow_books_url+"?lang=vi"
    else
      redirect_to borrow_books_url
    end
  
  end
  
end
